import pymongo
from datetime import datetime
from flask import Flask,render_template,url_for,redirect,request

app = Flask(__name__)
##数据库实例
connect=pymongo.MongoClient('127.0.0.1',27017)
db = connect.todo

class Todo(object):
    @staticmethod
    def cleate_doc(self,content):
        return{
            'content':content,
            'create_time':datetime.now(),
            'status':0,
            'finish_time':None
        }

@app.route('/')
def index():
    return redirect(url_for('get'))

@app.route('/get')
def get():
    todo_list = db.todo.find({})
    print(todo_list)
    return render_template('index.html',todo_list = todo_list)

@app.route('/add',methods=['POST'])
def add():
    form = request.form
    content = form['content']
    print(content)
    if content:
        affected_id =db.todo.insert_one({
            "content":content,
            "create_time":datetime.now(),
            "status":0,
            "finish_time":None
        })
        print(affected_id)
        if affected_id:
            return redirect(url_for('index'))

@app.route('/finish')
def finish():
    pass

@app.route('/delete')
def delete():
    args = request.args
    content = args['content']
    print(content)
    affect_id = db.todo.remove({
        'content':content
    })
    print(affect_id)
    return redirect(url_for('index'))

if __name__ == '__main__':
    app.run()
